package org.dandelion.core.channel
{
	import flash.utils.Dictionary;
	
	import mx.messaging.Channel;
	import mx.messaging.ChannelSet;
	import mx.messaging.config.ServerConfig;
	
	import org.dandelion.erro.DandelionError;
	import org.dandelion.erro.DandelionErrorInfo;

	public class ChannelSetFactory
	{
		
		/** */
		private static var _instance:ChannelSetFactory;
		
		private var _channelSets:Dictionary = new Dictionary();//ChannelSet的Map集合，存放各种ChannelSet
		
		
		
		public function ChannelSetFactory()
		{
			if(_instance) throw new Error("Error: Singletons can only be instantiated via instance() method!");
			ChannelSetFactory._instance = this;
		}
		
		/**
		 * 获取ChannelSetFactory实例
		 **/
		public static function getInstance():ChannelSetFactory
		{
			if (_instance == null) 
				_instance = new ChannelSetFactory();
			return _instance;
		}
		
		/**
		 * 获取一个ChannelSet
		 * @param name String service-config.xml中的channel的ID
		 * @return ChannelSet
		 * */
		public function getChannelSet(name:String):ChannelSet{
			if (_channelSets[name] == null){
				var channel:Channel = ServerConfig.getChannel(name);
				if(!channel){
					throw new DandelionError(DandelionErrorInfo.CHANNEL_NOT_FOUND);
				}
				var channelSet:ChannelSet = new ChannelSet();
				channelSet.addChannel(channel);
				_channelSets[name] = channelSet;
			}
			return _channelSets[name];
		}
		
	}
}